package com.eascs.crm.nsEditDetail.service;

import java.util.Date;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.dao.NsEditDetailDao;
import com.eascs.crm.nsEditDetail.entity.CarInfoEntity;
import com.eascs.crm.nsEditDetail.entity.CarInfoLogEntity;
import com.eascs.crm.nsEditDetail.entity.HouseInfoLogEntity;
import com.eascs.crm.nsEditDetail.form.CarInfoForm;
import com.eascs.web.sso.UserHolder;

@Service
public class CarInfoService {

	@Inject
	NsEditDetailDao nsEditDetailDao;

	public PageResult<Object[]> findCarListInfo(String loanapplyId,Integer showPage) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 10, showPage);
		StringBuilder sql = new StringBuilder();
		sql.append("select h.id,h.car_name,h.car_amount,h.car_data,h.is_mortgage,log.valuation,h.car_price,log.Car_Price Car_Price_review  from car_info h  left join Car_info_log log on h.id =log.Car_info_id  and log.op_date");
	    sql.append( "  in (select max(ll.op_date) from Car_info_log ll where ll.Car_info_id=log.Car_info_id) where 1=1 and h.is_del = 'N' ");
		if (StringUtil.isNotBlank(loanapplyId)) {
			sql.append(" and h.loanapply_no = ?");
			sqlPageQuery.addParam(loanapplyId);
		}
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> CarPage = nsEditDetailDao.findDataWithPage(sqlPageQuery);
		return CarPage;
	}
	
	public CarInfoEntity findCarInfoEntity(String CarId) {
		return nsEditDetailDao.getById(CarInfoEntity.class, CarId);
	}
	
	public CarInfoEntity findCarInfoByLoanapplyId(String loanapplyId) {
		return nsEditDetailDao.findEntity("select * from Car_info where loanapply_no = ?",
				CarInfoEntity.class, loanapplyId);
	}
	
	public CarInfoLogEntity findCarLogEntity(String carId) {
		return nsEditDetailDao.findEntity("select * from car_info_log l where l.op_date in (select MAX(t.op_date) from car_info_log t where t.car_info_id =l.car_info_id   group by t.car_info_id ) and l.car_info_id = ? ",
				CarInfoLogEntity.class, carId);
	}
	
	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public int  carDel(String carId){
		return nsEditDetailDao.execute("update  Car_info  set is_del ='Y' where id = ?",carId);
	}
	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public int carSave(CarInfoEntity Car,CarInfoForm form) {
		int i=0;
		setCarPorperty(Car, form);
		i = nsEditDetailDao.insert(Car);
		i=saveLog(Car);
		return i;
	}
	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public int carUpdate(CarInfoEntity Car,CarInfoForm form) {
		int i=0;
		setCarPorperty(Car, form);
		i=nsEditDetailDao.updateById(Car);
		i=saveLog(Car);
		return i;
	}

	private int saveLog(CarInfoEntity Car) {
		CarInfoLogEntity log= new CarInfoLogEntity();
		log.setCarInfoId(Car.getId());
		log.setOpPsn(UserHolder.getUser().getCname());
		log.setOpNo(UserHolder.getUser().getCode());
		log.setOpDate(new Date());
		log.setValuation(Car.getValuation());
		log.setCarPrice(Car.getCarPrice());
		return nsEditDetailDao.insert(log);
	}

	private void setCarPorperty(CarInfoEntity Car, CarInfoForm form) {
		Car.setCarName(form.getCarName());
		Car.setCarAmount(form.getCarAmount());
		Car.setCarData(form.getCarData());
		Car.setIsMortgage(form.getIsMortgage());
		Car.setValuation(form.getValuation());
		Car.setCarPrice(form.getCarPrice());
		Car.setIsDel("N");
		Car.setLoanapplyNo(StringUtil.isBlank(form.getLoanapplyId())?Car.getLoanapplyNo():form.getLoanapplyId());
		Car.setId(form.getCarId());
	}


	public <T> int insert(T entity) {
		return nsEditDetailDao.insert(entity);
	}

	public boolean checkCar(String loanapplyId){
		String sql="SELECT count(*) FROM car_info l INNER JOIN car_info_log log ON l.id = log.car_info_id WHERE log.op_date in (select MAX(t.op_date) from car_info_log t where t.car_info_id =log.car_info_id   group by t.car_info_id )   AND l.loanapply_no = ? and log.car_price is  null ";
		int i=nsEditDetailDao.findInteger(sql,loanapplyId);
		if(i>0){
			return true;
		}else{
			return false;
		}
	}
}
